<script>
import { requestGet, requestPost } from '@/api/system/common'

export default {
  name: 'NoticeClientAuthSetting',
  data() {
    return {
      show: false, // 是否显示
      saveObj: false,
      checkedAll: [], // 选择中内容项
      dataList: [],
      cid: null,
      type: '1'
    }
  },
  methods: {
    initDataList() {
      requestGet('rest/api/sys/notice/v1/temp/blurry', {})
        .then((res) => {
          const { data } = res
          this.dataList = data.data
        })
    },
    initChange() {
      requestGet('rest/api/sys/manager/v1/webhook/getAuth', { cid: this.cid, type: this.type })
        .then((res) => {
          const { data } = res
          if (data !== null) {
            this.checkedAll = data
          } else {
            this.checkedAll = []
          }
        })
    },
    openEntity(cid) {
      if (undefined === cid || cid === null) {
        this.$message.error('请选择要授权的客户端')
      } else {
        this.cid = cid
        this.initDataList()
        this.initChange()
        this.show = true
      }
    },
    // 执行保存
    submitCU() {
      const data = {
        list: this.checkedAll,
        clientId: this.cid,
        type: this.type
      }
      requestPost('rest/api/sys/manager/v1/webhook/auth', data)
        .then(res => {
          const { code } = res
          if (code === 200) {
            this.close()
          } else {
            this.$message.error(res.msg)
          }
        })
    },
    close() {
      this.show = false
      this.checkedAll = []
    }
  }
}
</script>
<template>
  <el-dialog
    append-to-body
    :close-on-click-modal="false"
    :before-close="close"
    :visible.sync="show"
    title="客户端权限管理"
    width="700px"
  >
    <div class="el-dialog-ul">
      <div class="el-ul-header">模版列表</div>
    </div>
    <el-checkbox-group
      v-model="checkedAll"
    >
      <el-checkbox v-for="item in dataList" :key="item.id" class="custom-checkbox" border :label="item.id"> {{ item.name }} </el-checkbox>
    </el-checkbox-group>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="close">取消</el-button>
      <el-button
        :loading="saveObj"
        type="primary"
        @click="submitCU"
      >保存
      </el-button>
    </div>
  </el-dialog>
</template>

<style scoped>
.el-dialog-ul {
  width: 100%;
  height: 60px;
  line-height: 60px;
  padding: 0 20px;
  box-sizing: border-box;
  background-color: #ecf8ff;
  border-radius: 4px;
  border-left: 5px solid #50bfff;
  color: #5e6d82;
}
.custom-checkbox {
  margin-top: 10px;
}
</style>
